//
//  ChooseAreaViewController.m
//  suRead
//
//  Created by colin on 2020/1/5.
//  Copyright © 2020 bao. All rights reserved.
//

#import "ChooseAreaViewController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "UIViewController+HUD.h"
#import "ServiceUserNew.h"

@interface ChooseAreaViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray<NSDictionary *> * area;
@property(nonatomic,strong)UITableView *infoTableView;
@end

@implementation ChooseAreaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIView *topBar = [UIView new];
    topBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBar];
    [topBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        if (iPhoneX) {
            make.top.equalTo(topBar.superview).offset(44.0f);
        } else {
            make.top.equalTo(topBar.superview).offset(20.0f);
        }
        make.height.equalTo(@44.0f);
    }];
    
    __weak typeof(self) weakSelf = self;
    UIButton *buttonBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonBack setImage:[UIImage imageNamed:@"button_backward"] forState:UIControlStateNormal];
    [buttonBack addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        //返回
        [weakSelf.navigationController popViewControllerAnimated:YES];
    }];
    [topBar addSubview:buttonBack];
    [buttonBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(buttonBack.superview);
        make.centerY.equalTo(buttonBack.superview);
        make.width.equalTo(@44.0f);
        make.height.equalTo(@44.0f);
    }];
    
    UILabel *labelTitle = [UILabel new];
    labelTitle.textColor = [UIColor blackColor];
    labelTitle.text = @"选择商圈";
    labelTitle.font = [UIFont fontWithName:@"FZBIAOYSJW--GB1-0" size:17.0f];
    [topBar addSubview:labelTitle];
    [labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(labelTitle.superview);
    }];
    
    UILabel *linelab = [UILabel new];
    linelab.backgroundColor = [UIColor colorWithRed:160/255.0f green:160/255.0f blue:160/255.0f alpha:1.0f];
    [topBar addSubview:linelab];
    [linelab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(topBar);
        make.height.equalTo(@0.5f);
    }];
    
    self.infoTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    if (@available(iOS 11.0, *)) {
        self.infoTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.infoTableView.contentInset = UIEdgeInsetsZero;
    self.infoTableView.estimatedSectionFooterHeight = CGFLOAT_MIN;
    self.infoTableView.estimatedSectionHeaderHeight = CGFLOAT_MIN;
    self.infoTableView.sectionHeaderHeight = CGFLOAT_MIN;
    self.infoTableView.sectionFooterHeight = CGFLOAT_MIN;
    self.infoTableView.tableFooterView = [UIView new];
    self.infoTableView.tableHeaderView = [UIView new];
    [self.infoTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    self.infoTableView.dataSource = self;
    self.infoTableView.delegate = self;
    
    [self.view addSubview:self.infoTableView];
    [self.infoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.infoTableView.superview);
        make.top.equalTo(topBar.mas_bottom);
    }];
    
    [self showHUDProgress:@""];
    [ServiceUserNew doGetArea:^(id  _Nonnull responseObject, NSString * _Nonnull message) {
        [weakSelf removeHUDProgress];
        [weakSelf setArea:responseObject];
        [weakSelf.infoTableView reloadData];
    } failure:^(NSString * _Nonnull message) {
        [weakSelf removeHUDProgress];
        [weakSelf showHUDText:message];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.area.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    /**
     "area_id" = 1;
     "create_time" = "2019-09-26 12:06:02";
     list = "<null>";
     name = "\U6b66\U660c\U533a";
     num = "<null>";
     num2 = "<null>";
     "recommend_id" = 44;
     status = 1;
     */
    cell.textLabel.text = [self.area[indexPath.row] objectForKey:@"name"];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if (self.complete) {
        self.complete(self.area[indexPath.row]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

@end
